import type { ApplyForReturnReq, HomeCategoryRes, HomeTagsItem, LossDetailRes, ProductListItem, ProductListReq } from '@/types/api/new'
import type {
  CancelOrRefundOrderReq,
  GeneratePayFineRes,
  GetAddressByJwdReq,
  GetAddressByJwdRes,
  JZQGetSignLink,
  NewTradePay,
  OrderItem,
  OrderListReq,
  OrderListRes,
  OrderServerItem,
  OrderServerReq,
  OverdueListReq,
  OverdueListRes,
  ReceivingAttachmentRequest,
  ReceivingAttachmentResponse,
  RiskControlAttachmentResponse,
  SubBillRes,
  UpdateReceivingAttachmentInfoRequest,
  UpdateRiskControlAttachmentRequest,
} from '@/types/api/order'
import type {
  AddComplaintReq,
  EditAddressItemReq,
  FetchAddressListReq,
  FetchAddressListRes,
  ListArticleRes,
  NewAddressItemReq,
  NewAddressItemRes,
  OcrCheckReq,
  OcrCheckRes,
  ParamConfigRes,
  QueryAntSignRes,
} from '@/types/api/other'
import type { ProductDetailRes } from '@/types/api/product'
import type { DictCodeRequest } from '@/types/api/public'
import type { recordAddRequest, RecordSearchRequest } from '@/types/api/record'
import type {
  AuthFaceReq,
  AuthFaceRes,
  ESignAuthFaceRes,
  GetESignFaceByOidRes,
  loginChangePasswordRequest,
  LoginRes,
  SaveIDCardImageReq,
  SaveIDCardReq,
  SetPhoneNumReq,
} from '@/types/api/user'
import { useJsonDeepParse } from '@/utils'
import ajax from './http'

const FormURLEncodedHeader = {
  header: {
    'content-type': 'application/x-www-form-urlencoded',
  },
}

export const userApi = {
  // login: (body: any) => ajax.post<never, LoginRes>(`/xyzl-os/open/app-user/login`, body),

  authFace: (body: AuthFaceReq) => ajax.post<never, AuthFaceRes>(`/xyzl-os/sys-common/app-user/authentication`, body),

  eSignAuthFace: (body: AuthFaceReq) => ajax.post<never, ESignAuthFaceRes>(`/xyzl-os/sys-common/e-face-auth/add`, body),

  getESignFaceByOid: (oid: number) => ajax.post<never, GetESignFaceByOidRes | null>(`/xyzl-os/sys-common/e-face-auth/getByOid/${oid}`),

  // startAPVerify

  refreshToken: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/app-user/refresh-token`, body),

  humanAuth: (id: string) => ajax.get<never, any>(`/xyzl-os/sys-common/app-order/${id}/humanAuth`),

  setPhoneNum: (query: SetPhoneNumReq) => ajax.get<never, string>(`/xyzl-os/sys-common/app-user/decryptPhoneNum`, query),

  h5SyncMobile: (query?: any) => ajax.get<never, any>(`/xyzl-os/sys-common/app-user/h5SyncMobile`, query),

  setUserInfo: (query?: any) => ajax.get<never, any>(`/xyzl-os/open/setUserInfo`, query),

  getAddress: () => ajax.get<never, any>(`/xyzl-os/sys-common/app-order/latest/address`),

  saveidCardImage: (body: SaveIDCardImageReq) => ajax.post<never, any>(`/xyzl-os/sys-common/app-user/save-idCard-image`, body),

  saveidCard: (body: SaveIDCardReq) => ajax.post<never, any>(`/xyzl-os/sys-common/app-user/save-idCard`, body),

  certifyResultQuery: (id: string) => ajax.get<never, any>(`/xyzl-os/sys-common/app-user/certifyResultQuery/${id}`),
  sms: (body?: any) => ajax.post<never, string>(`/xyzl-os/open/phone-code`, body, { ...FormURLEncodedHeader }),
  //

  captCha: () => ajax.get<never, any>('/bs-api/xyzl-org/open/captcha'),
  Login: (body?: any) => ajax.post<never, any>('/bs-api/auth-server/auth/login', body, { ...FormURLEncodedHeader }),
  changePassword: (body?: loginChangePasswordRequest) => ajax.post<never, any>('/bs-api/xyzl-org/sys-auth/change-password', body, { ...FormURLEncodedHeader }),
  verifyPassword: (password: string) => ajax.get<never, any>(`/bs-api/xyzl-org/sys-auth/check-password?passwordOld=${password}`),
  permission: () => ajax.get<never, any>('/bs-api/xyzl-org/sys-auth/has-perms'),
  //

}

export const recordApi = {
  page: (query: RecordSearchRequest) => ajax.get<never, any>('/bs-api/xyzl-os/risk-review-record/page', query),
  // 开放式增加
  ReviewAdd: (body: recordAddRequest) => ajax.post<never, any>('/bs-api/xyzl-os/open/risk-review-record/add', body),
  // 普通增加
  Add: (body: recordAddRequest) => ajax.post<never, any>('/bs-api/xyzl-os/risk-review-record/add', body),
  // 协议书
  LicenseAgreement: (id: any) => ajax.post<never, any>(`/bs-api/xyzl-os/open/be-risk-info-agreement/${id}`),
  // 初审
  ReviewRecord: (body: { id: number }) => ajax.post<never, any>(`/bs-api/xyzl-os/risk-review-record/query`, body),
}

export const productApi = {
  page: (body?: any) => ajax.post<never, any>(`/xyzl-os/open/app-product/page`, body),

  detail: (id: number) => ajax.post<never, ProductDetailRes>(`/xyzl-os/open/app-product/${id}/detail`)
    .then(x => useJsonDeepParse<ProductDetailRes>(JSON.stringify(x))),

  favorite: (id: number) => ajax.get<never, any>(`/xyzl-os/sys-common/app-product/${id}/collect`),

  favorites: (query?: any) => ajax.get<never, any>(`/app-product/favorites`, query),

  shops: (sid: number) => ajax.get<never, any>(`/xyzl-os/sys-common/shop/app/${sid}/shop-info`),

  shopGet: (query?: any) => ajax.get<never, any>(`/xyzl-system/open/shops/getOne`, query),

  shopsPage: (body?: any) => ajax.post<never, any>(`/xyzl-os/open/app-product/page`, body),

  listProductUse: (body?: any) => ajax.post<never, any>(`/xyzl-os/open/product-use`, body),
}

export const otherApi = {
  home: (query?: any) => ajax.get<never, any>(`/xyzl-system/open/app-product/app-list`, query),

  hetong: () => ajax.get<never, any>(`/xyzl-os/open/he-tong-empty`),

  material: () => ajax.get<never, any>(`/xyzl-system/sys-common/alipay/get-material`),

  shopslist: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/shops/list`, query),

  getParamConfig: (query?: any) => ajax.get<never, ParamConfigRes>(`/xyzl-os/open/get-param-config`, query),

  config: (query?: any) => ajax.get<never, any>(`/xyzl-system/open/mkx-config/list`, query),

  edit: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/mkx-config/edit`, body),

  merchant: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/alipay/merchant-order-sync`, query),

  complaint: (body?: AddComplaintReq) => ajax.post<never, void>(`/xyzl-system/sys-common/mkx-complaint/add`, body),

  products: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/product/list`, query),

  typeAPI: () => ajax.get<never, any>(`/xyzl-os/open/product-type/all`),

  couponUser: () => ajax.get<never, any>(`/xyzl-os/sys-common/coupon/queryPage/template`),

  saveAddress: (body?: NewAddressItemReq) => ajax.post<never, NewAddressItemRes>(`/xyzl-os/sys-common/user-address/add`, body),

  editAddress: (body?: EditAddressItemReq) => ajax.post<never, void>(`/xyzl-os/sys-common/user-address/edit`, body),

  shipDetail: (query?: any) => ajax.get<never, any>(`/xyzl-os/sys-common/user-address/list`, query),

  ocrCheck: (body: OcrCheckReq) => ajax.post<never, OcrCheckRes>(`/xyzl-os/sys-common/get-id-card-orc-info`, body),

  fetchAddress: (query?: FetchAddressListReq) => ajax.get<never, FetchAddressListRes>(`/xyzl-os/sys-common/user-address/list`, query),

  eventsGoods: () => ajax.get<never, any>(`/xyzl-os/open/appSysHome/find/eventsGoods`),

  openAppletCount: (query?: any) => ajax.get<never, any>(`/xyzl-os/open/openAppletCount`, query),

  listArticle: (query?: any) => ajax.get<never, ListArticleRes>(`/xyzl-os/open/c-article-type`, query),

  listH5Seo: (query?: any) => ajax.get<never, any>(`/xyzl-system/open/c-h5-seo/list`, query),
}

export const orderApi = {
  page: (body?: OrderListReq) => ajax.post<never, OrderListRes>(`/xyzl-os/sys-common/app-order/page`, body)
    .then(x => ({ ...x, data: useJsonDeepParse<OrderListRes['data']>(JSON.stringify(x.data)) })),

  znjedit: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/product-order-overdue-fine-edit`, body),

  extralist: (query?: OrderServerReq) => ajax.get<never, OrderServerItem[]>(`/xyzl-os/product-order-extra/list`, query),

  recyclepay: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/product-order-recycle/pay`, body),

  dyMessage: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/user/dy-message`, body),

  messageTemplateList: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/mkx-topic-message-template/list`, query),

  recyclerenew: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/product-order-recycle/renew`, body),

  couponlist: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/product-coupon/list`, query),

  couponuserlist: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/product-coupon-user/list`, query),

  couponadd: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/product-coupon-user/add`, body),

  refundget: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/alipay-refund-detail/get`, query),

  eqb: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/e-sign/e-sign-flow`, body),

  recycleget: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/product-order-recycle/get`, query),

  shopsenteradd: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/shops-enter/add`, body),

  commentlist: (query?: any) => ajax.get<never, any>(`/xyzl-system/open/product-order-comment/list`, query),

  refund: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/alipay-refund-detail/add`, body),

  pageorder: () => ajax.post<never, any>(`/xyzl-os/sys-common/app-order/page-order`),

  save: (body: Record<string, any>) => ajax.post<never, number>(`/xyzl-os/sys-common/app-order/save-order`, body),

  detail: (id: number) => ajax.get<never, OrderItem>(`/xyzl-os/sys-common/app-order/${id}/order-info`)
    .then(x => useJsonDeepParse<OrderItem>(JSON.stringify(x))),

  findonebyPid: (id: string) => ajax.get<never, any>(`/xyzl-os/open/find-one-by-pid/${id}`),

  canOrder: (oid: number, body: CancelOrRefundOrderReq) => ajax.post<never, any>(`/xyzl-os/sys-common/app-order/${oid}/cancel-order`, body, { ...FormURLEncodedHeader }),

  cancelcanOrder: (oid: number, body: CancelOrRefundOrderReq) => ajax.post<never, any>(`/xyzl-os/sys-common/app-order/${oid}/request-cancel-order-app`, body, { ...FormURLEncodedHeader }),

  reqCanOrder: (id: string, body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/app-order/${id}/cancel-order`, body),

  repeal: (id: string) => ajax.post<never, any>(`/xyzl-os/sys-common/app-order/${id}/request-cancel-order/repeal`),

  completepay: (id: string) => ajax.get<never, any>(`/xyzl-os/sys-common/app-order/${id}/complete`),

  recordPay: (oid: number, bid: number, jid: number = 0, body?: any, payType?: string) => ajax.post<never, string>(`/xyzl-os/sys-common/app-order/${oid}/pay-order/${bid}/${jid}/record?payType=${payType}`, body),

  /** 预授权  */
  freezeApi: (id: number, body?: any, payType?: string) => ajax.post<never, string>(`/xyzl-os/sys-common/app-order/${id}/freeze?payType=${payType}`, body),

  freeze1: (id: string) => ajax.post<never, any>(`/xyzl-os/sys-common/app-order/${id}/freeze1`),

  getKuaiDi: (query?: any) => ajax.get<never, any>(`/xyzl-os/sys-common/pd/getKuaiDi100`, query),

  receiving: (id: number) => ajax.get<never, any>(`/xyzl-os/sys-common/app-order/${id}/receiving`),

  couponedit: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/product-coupon-user/edit`, body),

  giveBack: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/app-order/giveBack`, body),

  recordadd: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/product-order-record/add`, body),

  buyout: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/product-order-recycle/buy-out`, body),

  orderBuyout: (oid: number, body?: any) => ajax.post<never, string>(`/xyzl-os/sys-common/app-order/${oid}/buyout`, body),

  generatePayFine: (body: { oid: number }) => ajax.post<never, GeneratePayFineRes>(`/xyzl-os/sys-common/product-order-overdue-fine/generate-pay-fine`, body),

  newTradePay: (body: NewTradePay) => ajax.post<never, { trade_no: string }>(`/xyzl-os/sys-common/ali-pay/new-alipay-trade`, body),

  jspay: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/alipay/submit-product-order-pay`, body),

  overdueList: (query: OverdueListReq) => ajax.get<never, OverdueListRes>(`/xyzl-os/sys-common/product-order-overdue-fine/list`, query),

  sign: (oid: number) => ajax.get<never, any>(`/xyzl-os/sys-common/app-antBlockchain/${oid}/getUrl`),

  ordervip: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/alipay/submit-vip-order`, body),

  editordervip: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/product-vip-order/edit`, body),

  getinfo: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/product-vip-info/get`, query),

  lclist: () => ajax.get<never, any>(`/xyzl-os/sys-common/shops-supplier/list`),

  addzt: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/shops-user-out-goods/add`, body),

  getzt: (query?: any) => ajax.get<never, any>(`/xyzl-os/sys-common/shops-user-out-goods/get`, query),

  ztedit: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/shops-user-out-goods/edit`, body),

  deliver: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/product-order/self-deliver`, body),

  signature: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/e-sign-flow-signature`, body),

  recordPeriodList: (body?: number[]) => ajax.post<never, SubBillRes>(`/xyzl-os/sys-common/app-order/record-period-list`, body),

  getAddress: () => ajax.get<never, any>(`/xyzl-org/open/get-address-tree`),

  cardList: () => ajax.get<never, any>(`/xyzl-os/sys-common/efb/sign-list`),

  bandCard: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/efb/confirm`, body),

  getValidCode: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/efb/sign`, body),

  getJwd: () => ajax.get<never, any>(`/xyzl-os/open/jwd`),

  getAddressByJwd: (query: GetAddressByJwdReq) => ajax.get<never, GetAddressByJwdRes>(`/xyzl-os/open/jwd-address`, query),

  offlineTypeList: (query?: any) => ajax.get<never, any>(`/xyzl-os/open/product-offline-type/list`, query),

  productOfflineList: (query?: any) => ajax.get<never, any>(`/xyzl-os/sys-common/app-order/product-offline/list`, query),

  productOfflineGet: (query?: any) => ajax.get<never, any>(`/xyzl-os/open/product-offline/getById`, query),

  queryAntSign: (oid: number) => ajax.post<never, QueryAntSignRes>(`/xyzl-os/sys-common/new-ant/queryAntSign/${oid}`),

  queryContractFlow: (oid: number) => ajax.post<never, any>(`/xyzl-os/sys-common/query-contract-flow/${oid}`),

  jzqGetSignLink: (body: JZQGetSignLink) => ajax.post<never, any>(`/xyzl-os/sys-common/jzq-sign/jzq-get-sing-link`, body, { ...FormURLEncodedHeader }),

  jzqGetSignLinkPhone: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/jzq-sign/phone-jzq-get-sing-link`, body, { ...FormURLEncodedHeader }),

  jzqGetSignLinkPromise: (body: JZQGetSignLink) => ajax.post<never, any>(`/xyzl-os/sys-common/jzq-sign/promise-jzq-get-sing-link`, body, { ...FormURLEncodedHeader }),

  getRiskControlAttachment: (oid: number) => ajax.get<never, RiskControlAttachmentResponse>(`/xyzl-os/sys-common/app-material-supplement/risk/${oid}`),

  updateRiskControlAttachment: (oid: number, body: UpdateRiskControlAttachmentRequest) => ajax.post<never, any>(`/xyzl-os/sys-common/app-material-supplement/risk/update/${oid}`, body),

  getReceivingAttachment: (query: ReceivingAttachmentRequest) => ajax.get<never, ReceivingAttachmentResponse>(`/xyzl-os/sys-common/app-material-supplement/receipt`, query),

  updateReceivingAttachment: (body: UpdateReceivingAttachmentInfoRequest) => ajax.post<never, any>(`/xyzl-os/sys-common/app-material-supplement/receipt/update`, body),
}

export const fenxiaoApi = {
  relation: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/sell-person-relation/get`, query),

  relationadd: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/sell-person-relation/add`, body),

  paysuccess: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/product-order/sell-pay-success`, query),

  yeji: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/product-order/list-order-statistics`, query),

  yejisum: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/product-order/my-order-sum`, query),

  tixian: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/sell-withdraw-record/add`, body),

  teamdadta: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/sell-person-relation/count-sum`, query),

  teamlist: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/sell-person-relation/list`, query),

  txlist: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/sell-withdraw-record/list`, query),

  yejis: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/product-order/list-order-statistics-one-person`, query),

  sellAuthor: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/sell-author/list`, query),

  sellAuthoradd: (body?: any) => ajax.post<never, any>(`/xyzl-system/sys-common/sell-author/add`, body),

  listall: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/sell-person-relation/list-all`, query),

  del: (query?: any) => ajax.get<never, any>(`/xyzl-system/sys-common/sell-author/delById`, query),

  generateAlipayQRCode: (body?: any) => ajax.post<never, any>(`/xyzl-os/sys-common/generateAlipayQRCode`, body),
}

export const newApi = {
  homeCategory: () => ajax.get<never, HomeCategoryRes>(`/xyzl-os/open/app/category`),

  homeTags: () => ajax.get<never, HomeTagsItem[]>(`/xyzl-os/open/app/tags`),

  productList: (query: ProductListReq) => ajax.get<never, ProductListItem[]>(`/xyzl-os/open/app/product/list`, query),

  applyForReturn: (body: ApplyForReturnReq) => ajax.post<never, any>(`/xyzl-os/sys-common/app-order/giveBack`, body),

  lossDetail: (oid: number) => ajax.get<never, LossDetailRes>(`/xyzl-os/sys-common/app-order/giveBack/info/${oid}`),
}
export const dictApi = {
  dictList: (query?: DictCodeRequest) => ajax.get<never, any>(`/xyzl-org/sys-common/list-dict-item`, query),
}
